const cookie = new Cookie();
//当用户选择记住密码时，下一次进入此页面将自动填充用户名和密码
const oldName = cookie.getCookie('username');
const oldPass = cookie.getCookie('password');
if (oldName !== undefined && oldPass !== undefined) {
  $('#inputName3').val(oldName)
  $('#inputPassword3').val(oldPass)
}

//登录事件
$('#login-btn').click(function () {
  //获取用户名和密码
  const username = $('#inputName3').val();
  const password = $('#inputPassword3').val();
  //用户非空判断
  if (username.length == 0) {
    $('.name-tip').text('请输入您的用户名')
  } else {
    //清空上一次提示信息
    $('.name-tip').empty();
    //密码非空验证
    if (password.length == 0) {
      $('.pass-tip').text('请输入您的密码')
    } else {
      //清空上一次提示信息
      $('.pass-tip').empty();
      //ajax请求验证用户名密码是否正确
      request({
        type: 'post',
        url: 'https://api.it120.cc/conner/user/username/login',
        data: {
          deviceId: '3396',
          deviceName: 'window',
          username,
          pwd: password
        },
        loading: true,
        timeout: 3000
      }).then(res => {
        //当用户勾选了记住密码，将密码存到cookie中
        if ($('#rember-me').prop('checked')) {
          cookie.setCookie('username', username, 10)
          cookie.setCookie('password', password, 10)
          cookie.setCookie('token', res.data.token, 10)
          cookie.setCookie('uid', res.data.uid, 10)
        } else {
          cookie.delCookie('username')
          cookie.delCookie('password')
          cookie.delCookie('token')
          cookie.delCookie('uid')
        }
        hsycms.success("账号密码正确，即将跳转到首页", () => { 
          location.href = './index.html' 
        }, 1800)
      })
    }
  }
})